package com.bubble.base.common.utlis;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * 分页工具类
 */
public class Paginator implements Serializable {

    private long total = 0l;           //总记录数
    private int page = 1;              //当前页数
    private int rows = 15;             //每页记录数
    private long totalPage = 1;        //总页数
    private String sortName;           //排序名称
    private String sortOrder;          //排序类型（DESC：降序，ASC：降序）
    private String orderby;            //排序字符串：ID DESC
    private String queryString;        //当前页所有参数

    public static void main(String[] args) {
        Paginator p = new Paginator(88) ;
        System.out.println("总记录数："+p.getTotal());
        System.out.println("当前页："+p.getPage());
        System.out.println("每页记录数："+p.getRows());
        System.out.println("总页数："+p.getTotalPage());
    }

    public Paginator() {
    }

    public Paginator(HttpServletRequest request) {
        init(request);
    }

    public Paginator(long total) {
        this.total = total;
    }

    public Paginator(long total, int page, int rows) {
        this.total = total;
        this.page = page;
        this.rows = rows;
    }

    public Paginator(long total, int page, int rows, String orderby) {
        this.total = total;
        this.page = page;
        this.rows = rows;
        this.orderby = orderby;
    }

    public Paginator(long total, int page, int rows, String sortName, String sortOrder) {
        this.total = total;
        this.page = page;
        this.rows = rows;
        this.sortName = sortName;
        this.sortOrder = sortOrder;
    }

    private void init(HttpServletRequest request) {
        String page = request.getParameter("page");
        String rows = request.getParameter("rows");
        String sortName = request.getParameter("sortName");
        String sortOrder = request.getParameter("sortOrder");
        String orderby = request.getParameter("orderby");
        this.setQueryString(request.getQueryString());

        if(CommonUtils.isNotEmpty(page)) {
            setPage(Integer.parseInt(page));
        }
        if(CommonUtils.isNotEmpty(rows)) {
            setRows(Integer.parseInt(rows));
        }
        if(CommonUtils.isNotEmpty(sortName)) {
            setSortName(sortName);
        }
        if(CommonUtils.isNotEmpty(sortOrder)) {
            setSortOrder(sortOrder);
        }
        if(CommonUtils.isNotEmpty(orderby)) {
            setOrderby(orderby);
        }
        if(CommonUtils.isNotEmpty(sortName) && CommonUtils.isNotEmpty(sortOrder)) {
            setOrderby(this.sortName + " " + this.sortOrder);
        }
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public long getTotalPage() {
        if(totalPage == 1) {
            return this.totalPage = (total % rows) == 0 ? (total / rows) : ((total / rows) + 1) ;
        } else {
            return this.totalPage = totalPage;
        }
    }

    public void setTotalPage(long totalPage) {
        this.totalPage = totalPage;
    }

    public String getSortName() {
        return sortName;
    }

    public void setSortName(String sortName) {
        this.sortName = sortName;
    }

    public String getSortOrder() {
        return sortOrder;
    }

    public void setSortOrder(String sortOrder) {
        this.sortOrder = sortOrder;
    }

    public String getOrderby() {
        return orderby;
    }

    public void setOrderby(String orderby) {
        this.orderby = orderby;
    }

    public String getQueryString() {
        return queryString;
    }

    public void setQueryString(String queryString) {
        this.queryString = queryString;
    }
}
